//
//  BPBaseRequest.swift
//  BPAppCliect
//
//  Created by lichentao on 15/1/16.
//  Copyright (c) 2015年 lichentao. All rights reserved.
//  网络请求api

import Foundation

typealias SuccessBlock = (responDictionary: NSDictionary!) -> ()
typealias FailureBlock = (responErrorDictionary:NSDictionary!) -> ()
typealias BoolSuccessBlock = (success: Bool) -> ()

class BPBaseRequest: NSObject {

    // 创建单例
    class func shareInstance()->BPBaseRequest{
        struct BPSingleton{
            static var predicate:dispatch_once_t = 0
            static var instance:BPBaseRequest? = nil
        }
        dispatch_once(&BPSingleton.predicate,{
            BPSingleton.instance=BPBaseRequest()
            }
        )
        return BPSingleton.instance!
    }
    
    var requestInstance :AFHTTPRequestOperationManager!

    // post 请求
    func requestPostWithParamerCallBlock(path : String, parameter : NSDictionary!, callSuccessBack:SuccessBlock?, callFailureBlock:FailureBlock?){

        let finalPath = PATH_BASE.stringByAppendingString(path)
        requestInstance = AFHTTPRequestOperationManager()
        requestInstance.responseSerializer.acceptableContentTypes =  NSSet().setByAddingObject("text/html")

        print("final Path is \(finalPath)", terminator: "")
        requestInstance.POST(path,
             parameters: parameter ,
                success: {
                (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
                    
                    let callBackDictionary : NSDictionary? = responseObject as! NSDictionary!
                    
                    NSLog("callBackDictionary is\(callBackDictionary)")
                    let errorCode: AnyObject? = callBackDictionary?.objectForKey("error_code")!
                    
                    if (errorCode?.integerValue == 0){
                        callSuccessBack!(responDictionary: callBackDictionary)
                    }
            },
                failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
<<<<<<< HEAD
//                    let params = ["errorMsg": error.description, "errorCode": error.description)]
=======
//                    let params = ["errorMsg": error.description, "errorCode": error.encode()]
>>>>>>> 288d846b1206e88894e02a6ccf2ace272f888b1f
//                    callFailureBlock!(responErrorDictionary: params)
            })
    }
    
    // get请求
    func requestGetWithParamerCallBlock(path : String, parameter : NSDictionary!, callSuccessBack:SuccessBlock?, callFailureBlock:FailureBlock?){
        
        let finalPath = PATH_BASE.stringByAppendingString(path)
        requestInstance = AFHTTPRequestOperationManager()
        requestInstance.responseSerializer.acceptableContentTypes =  NSSet().setByAddingObject("text/html")
        
        requestInstance.GET(finalPath,
            parameters: parameter ,
            success: {
                (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
                
                var callBackDictionary : NSDictionary? = responseObject as! NSDictionary!
                
                NSLog("callBackDictionary is\(callBackDictionary)")
                var errorCode: AnyObject? = callBackDictionary?.objectForKey("error_code")!
                
                if (errorCode?.integerValue == 0){
                    callSuccessBack!(responDictionary: callBackDictionary)
                }
            },
            failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
<<<<<<< HEAD
                let params = ["errorMsg": error.description, "errorCode": error.description]
                print("error is \(error)")
                callFailureBlock!(responErrorDictionary: params)
=======
//                let params = ["errorMsg": error.description, "errorCode": error.encode()]
//                print("error is \(error)")
//                callFailureBlock!(responErrorDictionary: params)
>>>>>>> 288d846b1206e88894e02a6ccf2ace272f888b1f
        })
    }
    
    // 成功 || 失败 post
    func requestPostWithParamerSuccessFailBlock(path : String , parameter: NSDictionary,callBoolBlock : BoolSuccessBlock? , callFailureBlock : FailureBlock?){
        
        let finalPath = PATH_BASE.stringByAppendingString(path)
        requestInstance = AFHTTPRequestOperationManager()
        requestInstance.responseSerializer.acceptableContentTypes =  NSSet().setByAddingObject("text/html")
        
        requestInstance.POST(finalPath,
            parameters: parameter ,
            success: {
                (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
                
                let callBackDictionary : NSDictionary? = responseObject as! NSDictionary!
                NSLog("callBackDictionary is\(callBackDictionary)")
                let errorCode: AnyObject? = callBackDictionary?.objectForKey("error_code")!
                
                if (errorCode?.integerValue == 0){
                    
                    callBoolBlock!(success: errorCode!.boolValue)
                    
                }else{
                    callFailureBlock!(responErrorDictionary: callBackDictionary)
                }
            },
            failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
                
                let params = ["error_msg": error.description]
                callFailureBlock!(responErrorDictionary: params)
        })
    }
    
}